<?php

/*
 * Project: oCoder Framework
 * File: /controllers/user.php
 * Purpose: controller for the user of the app.
 * Author: Trung Hoang
 */

class baivietController extends BaseController {

    //add to the parent constructor
    public function __construct($action, $urlValues) {
        //Check login
        // if(!Helper::isLogin()){
        //     //save current url: after login, auto redirect client to current url
        //     $_SESSION['lastUrl'] = Helper::currentPageURL();
        //     //redirect to login page
        //     header( 'Location: '.BASE_URL.'account/login' ) ;
        // }
         if(!Helper::isLogin()){
            //save current url: after login, auto redirect client to current url
            $_SESSION['lastUrl'] = Helper::currentPageURL();
            //redirect to login page
            header( 'Location: '.BASE_URL.'account/login' ) ;
        }
        parent::__construct($action, $urlValues);

        //create the model object
        require("models/baiviet.php");
        $this->model = new baivietModel();
    }

    //default action
    protected function index() {
        if(Helper::Authenticate('XemBaiViet'))
        {
            if (!empty($_GET['id'])) {
                $para = $_GET['id'];
            } else {
                $para = 1;
            }
            $this->view->output_admin($this->model->index($para));
        }
        else Helper::redirect();
    }

    protected function category() {
        if (!empty($_GET['id'])) {
            $para = $_GET['id'];
        } else {
            $para = 'tin-tuc';
        }
        $this->view->output($this->model->category($para));
    }
    protected function detail()
    {                
        if(!empty($_GET['id']))
        {
             $alias = $_GET['id'];             
        }
        else
        {
            header( 'Location: BASE_URL' ) ;
        }    
        if(!isset($_SESSION['view_article']) or $_SESSION['view_article']!=$alias)          
        {       
            $result = $this->model->update_view_article($alias);       
            $_SESSION['view_article'] = $alias;

        } 
        $this->view->output($this->model->detail($alias)); 
    }
    //create action
    protected function create() {
        if(Helper::Authenticate('ThemBaiViet'))
        {
            //load data post from client
            if (!empty($_POST)) {
                //save item and return id of item created
                if (empty($_POST['alias'])) {
                    $_POST['alias'] = Helper::ToAlias($_POST['tenbaiviet']);
                }

                if (!empty($_POST['danhmuc'])) {
                    $_POST['TuKhoaDanhMuc'] = Helper::ToAlias($_POST['TenDanhMuc']);
                }
                $TuKhoaBaiViet = Helper::ToAlias($_POST['TieuDe']);
                if (isset($_SESSION['userLogin'])){
                    $user = $_SESSION['userLogin'];
                }
                else
                {
                    $user = 1;
                }
                $data1 = array(
                    'AnhThumb' => $_POST['hinhanh'],
                    'DuongDan' => $_POST['alias'],
                    //'Id'
                    //'IdNguoiCapNhat' => $user,
                    //'IdNguoiTao' => $user,
                    'MoTa' => $_POST['mota'],
                    //'NgayCapNhat' => $_POST[''],
                    //'NgayTao' =>getdate(), 
                    'NoiDung' => $_POST['noidung'],
                    'TieuDe' => $_POST['tenbaiviet'],
                    'TinhTrang' => $_POST['tinhtrang'],
                    'TuKhoaBaiViet' => $_POST['alias'],
                    'TuKhoaDanhMuc' => $_POST['danhmuc'],
                    'TuKhoaDiaDiem' => $_POST['tukhoadiadiem'],
                    'TuKhoaTinhThanh' => $_POST['tukhoatinhthanh'],
                    'Clip' => $_POST['clip']

                        //'TuKhoaTinhThanh' => $_POST['TenTinh'], 
                );
                // var_dump($data1);die;
                $result = $this->model->create($data1);
                if ($result) {
                    $alert = array(
                        'class' => 'alert-success',
                        'message' => 'Thêm bài viết thành công'
                    );
                    $_SESSION['message'] = $alert;
                    header('Location: ' . BASE_URL . 'baiviet/index');
                }
            }
            //load form create
            $this->view->output_admin($this->model->create(''));
        }
        else Helper::redirect();
    }

    //update action
    protected function update() {
        if(Helper::Authenticate('SuaBaiViet'))
        {
            //load data update post from client
            if (!empty($_POST)) {
                
                //get data from POST
                $id = $_POST['id'];
                $alias = Helper::ToAlias($_POST['tenbaiviet']);
                $TuKhoaDanhMuc = Helper::ToAlias($_POST['TenDanhMuc']);
                $TuKhoaBaiViet = Helper::ToAlias($_POST['tenbaiviet']);
                if (isset($_SESSION['userLogin'])){
                    $user = $_SESSION['userLogin'];
                }
                else
                {
                    $user = 1;
                }
                
                $data1 = array(
                    'AnhThumb' => $_POST['hinhanh'],
                    'DuongDan' => $alias,
                    //'Id'
                    //'IdNguoiCapNhat' => $user,
                    //'IdNguoiTao' => $_POST['idnguoitao'],
                    'MoTa' => $_POST['mota'],
                    //'NgayCapNhat' => $_POST[''],
                    //'NgayTao' =>getdate(), 
                    'NoiDung' => $_POST['noidung'],
                    'TieuDe' => $_POST['tenbaiviet'],
                    'TinhTrang' => $_POST['tinhtrang'],
                    'TuKhoaBaiViet' => $TuKhoaBaiViet,
                    'TuKhoaDanhMuc' => $TuKhoaDanhMuc,
                    'TuKhoaDiaDiem' => $_POST['tukhoadiadiem'],
                    'TuKhoaTinhThanh' => $_POST['tukhoatinhthanh'],
                        //'TuKhoaTinhThanh' => $_POST['TenTinh'], 
                );
                //update item -> return total rows affected
                $result = $this->model->edit($id, $data1);
                if ($result) {
                    $alert = array(
                        'class' => 'alert-success',
                        'message' => 'Cập nhật thông tin bài viết thành công'
                    );
                    $_SESSION['message'] = $alert;
                    header('Location: ' . BASE_URL . 'baiviet/index');
                }
            }
            //load form update
            $this->view->output_admin($this->model->edit($id = $this->urlValues['id'], $data = ''));
        }
        else Helper::redirect();
    }

    //delete action
    protected function delete() {
        if(Helper::Authenticate('XoaBaiViet'))
        {
            //when client delete multi item
            if (!empty($_POST)) {
                $result = $this->model->delete('bv_baiviet', 'id IN(' . implode(',', $_POST['id']) . ')');
            } else {
                $result = $this->model->delete('bv_baiviet', 'id=' . $this->urlValues['id']);
            }

            if ($result) {
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Đã xóa địa điểm'
                );
                $_SESSION['message'] = $alert;
            }

            header('Location: ' . BASE_URL . 'baiviet/index');
        }
        else Helper::redirect();
    }

    protected function deleteimage() {
        if(Helper::Authenticate('XoaAnhBaiViet'))
        {
            //when client delete multi item
            if (!empty($_POST)) {
                $result = $this->model->delete('dd_media', 'id IN(' . implode(',', $_POST['id']) . ')');
            } else {
                $result = $this->model->delete('dd_media', 'id=' . $this->urlValues['id']);
            }

            if ($result) {
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Đã xóa hình ảnh'
                );
                $_SESSION['message'] = $alert;
            }

            header('Location: ' . BASE_URL . 'baiviet/index');
        }
        else Helper::redirect();
    }

}

?>